#!/bin/bash

source /home/shell/util.sh

function install () {

  read -p "zookeeper地址[${host_addr}]：" zk_addr
  ${zk_addr:="${host_addr}"};

  read -p "zookeeper端口[2181]：" zk_port
  zk_port=${zk_port:-"2181"};

  if [ ! -d "/opt/canal/canal.admin" ]; then
    mkdir -p /opt/canal/canal.admin
  fi
  if [ ! -d "/opt/canal/canal.deployer" ]; then
    mkdir -p /opt/canal/canal.deployer
  fi

  printInfo "Canal版本列表："

  while [ true ]; do
    curl -sL https://hub.fastgit.org/alibaba/canal/releases|grep "pl-2 flex-auto min-width-0 text-bold"|perl -pe "s/.*<span.*?>(.*?)<\/span>/\1/g"|grep -v SNAPSHOT|perl -pe "s/.*-(.*).tar.gz/\1/g"|uniq
    read -p "请选择版本：" version
    if [[ -z "${version}" ]]; then
      printFail "请输入版本"
    else
      break;
    fi
  done
  
  
  download /opt/canal/canal.admin/canal.admin.tar.gz https://hub.fastgit.org/alibaba/canal/releases/download/canal-${version}/canal.admin-${version}.tar.gz
  download /opt/canal/canal.deployer/canal.deployer.tar.gz https://hub.fastgit.org/alibaba/canal/releases/download/canal-${version}/canal.deployer-${version}.tar.gz

  tar -xf /opt/canal/canal.admin/canal.admin.tar.gz -C /opt/canal/canal.admin
  tar -xf /opt/canal/canal.deployer/canal.deployer.tar.gz -C /opt/canal/canal.deployer
  rm -rf /opt/canal/canal.admin/canal.admin.tar.gz
  rm -rf /opt/canal/canal.deployer/canal.deployer.tar.gz

  if [ ! -d "/opt/zookeeper/bin" ]; then
    read -p "是否安装zookeeper？[N]" zookeeper_install
    zookeeper_install=${zookeeper_install:-"N"}
    case "${zookeeper_install}" in
      y|Y)
        bash /home/shell/zookeeper-install.sh
      ;;
      *)
      ;;
    esac
  fi
}

function managerConfig () {
  requireJava
  requireMysqlCient

  read -p "Canal数据库地址[${host_addr}]：" mysql_url
  mysql_url=${mysql_url:-"${host_addr}"};

  read -p "Canal数据库端口[3306]：" mysql_port
  mysql_port=${mysql_port:-"3306"};

  read -p "Canal数据库用户名[dev_user]：" mysql_username
  mysql_username=${mysql_username:-"dev_user"};

  read -p "Canal数据库密码[123456]：" mysql_passwd
  mysql_passwd=${mysql_passwd:-"123456"};


expect <<EOF
    spawn mysql_config_editor set --login-path=canal_manager_dev_host --host=${mysql_url} --port=${mysql_port} --user=${mysql_username} --password
    expect {
        "password" {send "$mysql_passwd\n";exp_continue}
        "exists" {send "y\n";interact}
    }
EOF

mysql --login-path=canal_manager_dev_host<<EOF

source /opt/canal/canal.admin/conf/canal_manager.sql;
CREATE USER IF NOT EXISTS canal IDENTIFIED WITH mysql_native_password BY 'canal';
GRANT ALL PRIVILEGES ON canal_manager.* TO 'canal'@'%' with grant option;
FLUSH PRIVILEGES;

use canal_manager;
INSERT INTO canal_manager.canal_cluster(id, name, zk_hosts) VALUES (1, 'mycluster', '${zk_addr}:${zk_port}');

EOF

  sed -i "/## set java path/a\JAVA=/opt/jdk/bin/java" /opt/canal/canal.admin/bin/startup.sh

}


function connectMaster () {
  printInfo "连接到MySQL数据库"

  read -p "Master数据库地址[${host_addr}]：" master_mysql_url
  master_mysql_url=${master_mysql_url:-"${host_addr}"};

  read -p "Master数据库端口[3306]：" master_mysql_port
  master_mysql_port=${master_mysql_port:-"3306"};

  read -p "Master数据库用户名[canal]：" master_mysql_username
  master_mysql_username=${master_mysql_username:-"canal"};

  read -p "Master数据库密码[canal]：" master_mysql_passwd
  master_mysql_passwd=${master_mysql_passwd:-"canal"};

  read -p "Canal管理员账号[admin]：" admin_user
  admin_user=${admin_user:-"admin"};

  read -p "Canal管理员账号的密码[admin]：" admin_passwd
  admin_passwd=${admin_passwd:-"admin"};

  read -p "Canal WEB服务端口[8089]：" server_port
  server_port=${server_port:-"8089"};

  sed -ri "s/address: 127.0.0.1:3306/address: "${master_mysql_url}":"${master_mysql_port}"/g" /opt/canal/canal.admin/conf/application.yml
  sed -ri "s/username: canal/username: "${master_mysql_username}"/g" /opt/canal/canal.admin/conf/application.yml
  sed -ri "s/password: canal/password: "${master_mysql_passwd}"/g" /opt/canal/canal.admin/conf/application.yml
  sed -ri "s/adminUser: admin/adminUser: "${admin_user}"/g" /opt/canal/canal.admin/conf/application.yml
  sed -ri "s/adminPasswd: admin/adminPasswd: "${admin_passwd}"/g" /opt/canal/canal.admin/conf/application.yml
  sed -ri "s/port: 8089/port: "${server_port}"/g" /opt/canal/canal.admin/conf/application.yml

  firewall-addport ${server_port}
}

function deployerConfig () {
  read -p "canal admin地址[${host_addr}]：" canal_admin_addr
  canal_admin_addr=${canal_admin_addr:-"${host_addr}"};

  sed -ri "s/canal.admin.register.cluster =/canal.admin.register.cluster = mycluster/g" /opt/canal/canal.deployer/conf/canal_local.properties
  sed -ri "s/canal.register.ip =/canal.register.ip = "${host_addr}"/g" /opt/canal/canal.deployer/conf/canal_local.properties
  sed -ri "s/canal.admin.manager = 127.0.0.1:8089/canal.admin.manager = "${canal_admin_addr}":"${server_port}"/g" /opt/canal/canal.deployer/conf/canal_local.properties
  sed -ri "s/canal.admin.register.name =/canal.admin.register.name = deployer1/g" /opt/canal/canal.deployer/conf/canal_local.properties

  sed -i "/## set java path/a\JAVA=/opt/jdk/bin/java" /opt/canal/canal.deployer/bin/startup.sh
}

function managerun () {
bash /home/shell/serviced-install.sh<<EOF
2


canaladmin
/opt/canal/canal.admin/bin/startup.sh


/opt/canal/canal.admin/bin/stop.sh

EOF
}

function deployerun () {
bash /home/shell/serviced-install.sh<<EOF
2


canaldeployer
/opt/canal/canal.deployer/bin/startup.sh local


/opt/canal/canal.deployer/bin/stop.sh

EOF

while [ true ]; do
  
  if [ -f "/opt/canal/canal.admin/logs/admin.log" ]; then
    result=`tail /opt/canal/canal.admin/logs/admin.log|grep "Tomcat started"`;
  fi
  
  if [[ -n "${result}" ]]; then
    break;
  fi
  loadMsg "canal admin启动中..."
  sleep 1s
done

printInfo "========================canal========================"
printInfo "admin控制台地址：http://${host_addr}:${server_port}"
printInfo "admin控制台账号：admin"
printInfo "admin控制台初始密码：123456"
printInfo "========================canal========================"
}


install && managerConfig && connectMaster && managerun

deployerConfig && deployerun